package cn.iocoder.yudao.module.iot.gateway.protocol.custom.xiaoan.tcp.codec;

import cn.iocoder.yudao.framework.common.core.ArrayValuable;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@Getter
@AllArgsConstructor
public enum XiaoAnCmdTypeEnum implements ArrayValuable<Byte> {
    WILD((byte) 0x00, "透传"),
    LOGIN((byte) 0x01, "登录"),
    PING((byte) 0x02, "心跳");
    private final byte code;
    private final String description;

    // 通过Map实现快速查找
    private static final Map<Byte, XiaoAnCmdTypeEnum> CODE_MAP = new HashMap<>();

    static {
        for (XiaoAnCmdTypeEnum cmdType : values()) {
            CODE_MAP.put(cmdType.code, cmdType);
        }
    }
    public static final Byte[] ARRAYS = Arrays.stream(values()).map(XiaoAnCmdTypeEnum::getCode).toArray(Byte[]::new);

    /**
     * 根据命令码获取对应的枚举值
     *
     * @param code 命令码
     * @return 对应的枚举值，未找到返回null
     */
    public static XiaoAnCmdTypeEnum of(byte code) {
        return CODE_MAP.get(code);
    }

    @Override
    public Byte[] array() {
        return ARRAYS;
    }
}
